import config from './index'
import Url from 'browser-url'
import axios from 'axios'
import store from '../vuex'


export const basic = url => new Url(config.basic + url);
export const ingfrast=url=>new Url(config.ingfrast+url);

axios.defaults.timeout = 6000
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });
axios.interceptors.response.use(response => {
  if (response.status >= 200 && response.status < 300) {
    store.dispatch('hideloading')
    if (response.data.statusCode !== 200) {

    }
    return response.data
  } else {
    var error = new Error(response.statusText)
    error.response = response
    // throw error
    store.dispatch('hideloading')
  }
}, function (error) {
  store.dispatch('hideloading')
  //store.dispatch('error', error)
})

export const doget = (url, data={},options = { hideLoading: false }) => {
  store.dispatch('showloading')
  if (options.hideLoading) {
    //store.dispatch('showloading')
  } 
  
  url.addQuery(data)
  return axios.get(url, options)
}